//
//  ViewController.swift
//  BJ12366Swift
//
//  Created by ZRAR on 15/12/28.
//  Copyright © 2015年 ZRAR. All rights reserved.
//

import UIKit

class ViewController: UIViewController, SendValueDetegate{

    @IBOutlet weak var timeLabel: UILabel!
    
    @IBAction func buttonAction(sender: AnyObject) {
        let dateView = DatePickerView.ReadDatePickerView()
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        dateView.setDatePicker(formatter, nowDate: NSDate(), pickerMode: .Date)
        //传递函数
//        dateView.comfirm { (string) -> Void in
//            
//            self.timeLabel.text = string
//
//        }
        //直接赋值
        dateView.comfirClosure = { (string) -> Void in
            
            self.timeLabel.text = string
            
        }
        dateView.delegate = self
        dateView.showInView()
        
    }
    func valueChanged(newString: String) {
        self.timeLabel.text = newString
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var mathFunction:(Int,Int)->Int = addTwoInts
        print("Result:\(mathFunction(1,2))")
        mathFunction = multiplyTwoInts
        let anotherMathFunction = addTwoInts
        printMathReust(anotherMathFunction, 3, 5)
        
        let names = ["Chris","Alex","Ewa","Barry","Daniella"]
        var reversed = names.sort(backWards)
        print("\(reversed)")
        var sortNames = names.sort { (s1, s2) -> Bool in
            
            return s1 > s2
        }
        
        //单表达式闭包隐式返回
        sortNames = names.sort({s1,s2 in s1>s2})
        //参数名称缩写，可以直接通过$0,$1,$2来顺序调用闭包的参数，类推
        sortNames = names.sort({$0>$1})
        //运算符函数，String类型定义了关于大于号>的函数实现，可以简单的传递一个大于号
        sortNames = names.sort(>)
        someFunctionThatTakesAclosureClosure { () -> Void in
            
            
        }
        someFunctionThatTakesAclosureClosure(){
            
        }
        someFunctionThatTakesAclosureClosure({
        
            
        })
        let digitNames = [
                            0:"Zero",
                            1:"One",
                            2:"Two",
                            3:"Three",
                            4:"Four",
                            5:"Five",
                            6:"Six",
                            7:"Senven",
                            8:"Eight",
                            9:"Nine"
                        ]
        let numbers = [16,58,510]
        
        let strings = numbers.map { (var number) -> String in
            
            var output = ""
            while number>0{
                output = digitNames[number%10]!+output
                number /= 10
            }
            return output
        }
        let incrementor = makeIncrementor(forIncement: 5)
        //incrementor()
        print("\(incrementor())")
        let intcrementorSenven = makeIncrementor(forIncement: 7)
        print("\(intcrementorSenven())")
        
        let instance = Closures()
        let instance2 = instance
        if instance === instance2{
            //是否引用一个实例,!==不等价
            print("instance and instance2 also refer to the same Resoution instace")
        }else{
            print("not same resotion instance")
        }
        instance.doSomething()
        print(instance.x)
        instance.completionHandlers.first?()
        print(instance.x)
        instance.serveCustome { () -> String in
            
            return "viewController"
        }
        
        instance.serverautCustomers("@autoclosure")
        let enumeration = EnumerationsLearn()
        print("\(enumeration.directionToHead)")
        
        let properties = Properties()
        properties.stepCounter.totalSteps = 200;
        let  nowDate = NSDate()
        print("今年是\(nowDate.year)年");
     
    }
    
    func someFunctionThatTakesAclosureClosure(closure:()->Void){
        let str = "12"
        let int = Int(str)
        let str1 = String(int)
        
    }
    
    func backWards(s1:String,s2:String)->Bool{
        
        return s1 > s2
    }
    
    func printMathReust(mathFunction:(Int,Int)->Int,_ a:Int,_ b:Int){
        
        print("\(mathFunction(1,b))")
    }
    
    func addTwoInts(a:Int,_ b:Int) ->Int{
        return a+b
    }
    func multiplyTwoInts(a:Int,_ b:Int) ->Int{
        
        return a*b
    }
    
    func makeIncrementor(forIncement amount:Int)->()->Int{
        var runingTotal = 0
        func incrementor()->Int{
            
            runingTotal += amount
            return runingTotal
        }
        
        return incrementor
    }
    //调用类型属性
    func someClassPropertiesTest(){
        
        print(SomeStructure.computedType)
        print(SomeEnum.computedType)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    
    }
    
}


struct SomeStructure {
    
    static var storeType = "Some value"
    static var computedType:Int{
        return 1
    }
    
}

enum SomeEnum{
    
    static var storeType = "Some value"
    static var computedType:Int{
        return 6
    }
}

class SomeClass {
    
    static var storeType = "Some value"
    static var computedType:Int{
        return 27
    }
    //在为类定义计算类型属性时，可以使用关键字class来支持子类对父类的实现进行重写。
    class var overrideableComputeType:Int{
        return 34
    }
}
